import os
import xlrd
import xlwt
import csv
# 查找电脑上的 .xls 文件
def find_xls_files(directory):
    xls_files = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.xls'):
                xls_files.append(os.path.join(root, file))
    return xls_files

# 读取 .xls 文件
def read_xls(file_path):
    workbook = xlrd.open_workbook(file_path)
    sheet = workbook.sheet_by_index(0)  # 读取第一个工作表
    content = []
    for row_idx in range(sheet.nrows):
        row = sheet.row(row_idx)
        row_data = [cell.value for cell in row]
        content.append(row_data)
    return content

# 写入 .xls 文件
def write_xls(file_path, content):
    workbook = xlwt.Workbook()
    sheet = workbook.add_sheet('Sheet1')  # 添加一个工作表
    for row_idx, row_data in enumerate(content):
        for col_idx, cell_data in enumerate(row_data):
            sheet.write(row_idx, col_idx, cell_data)
    workbook.save(file_path)

# 主函数
def main():
    directory = r'C:\Users\王相辰\Desktop\大一_实践教育中心'  
    xls_files = find_xls_files(directory)
    
    all_data = []

    # 处理 .xls 文件
    for xls_file in xls_files:
        content = read_xls(xls_file)
        all_data.append(['.xls', xls_file, content])
        # 写入一个新的 .xls 文件
        new_xls_file = xls_file.replace('.xls', '_new.xls')
        write_xls(new_xls_file, content)
        print(f"处理并保存了 {xls_file} 到 {new_xls_file}")

    # 保存所有数据到 CSV 文件
    with open('excel_documents_data.csv', 'w', newline='', encoding='utf-8') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['File Type', 'File Path', 'Content'])
        for data in all_data:
            # 将内容转换为字符串以便写入 CSV
            content_str = '\n'.join(['\t'.join(map(str, row)) for row in data[2]])
            writer.writerow([data[0], data[1], content_str])
    
    print("所有数据已成功保存到 excel_documents_data.csv 文件中")

if __name__ == "__main__":
    main()